//返回上一级
$('.back').click(function  () {
	//window.history.go(-1)
	location.href='personal.html'
})
//数据
var username=sessionStorage.getItem('username');
var img_url="http://47.92.37.168/supermarket/img/";
//生成全部订单
var arr_id=[];
var num=0;
$.ajax({
	type:"get"
	,url:"http://47.92.37.168/supermarket/data/get_commodity_order.php"
	,async:true
	,data:{
		'user_phone':username
	}
	,dataType:"jsonp"
	,jsonp:"callback"
	,success:function(data){
		console.log(data);
		html='';
		for(var i in data){
			html += '<li><a href="#"><div class="shopName"><div><img src="img/tmlogo.png" /><span>店铺名称&nbsp;&gt;</span></div><p class="jy">交易成功</p></div><div class="priceName"><div><img src="" class="content_img"/><span class="content_info" maxlength="10"></span><p class="content_price"><span class="price">0.00</span></p> </div> <p>共 <span style="color: orange" class="commodity_num">'+data[i].count+'</span> 件商品&emsp;&nbsp;&emsp;&nbsp;合计:&emsp;(含运费<span class="total_price">0.00</span>)</p></div> </a> <div class="deleteBtn"><p><span class="evaluate_order remove" id='+data[i].commodity_id+' time='+data[i].order_time+'>删除订单</span><a class="evaluate_order" id='+data[i].commodity_id+' consignee_name='+data[i].user_name+'>评价</a></p> </div></li>'
		arr_id.push(data[i].commodity_id);
		}
		
		$('.orderSection').append(html)

		console.log(arr_id);
		getProDetails(arr_id[num]);
	}
});
//获取商品图片、名称、单价的函数
function getProDetails(pro_id) {
	$.ajax({
		type:"get"
		,url:"http://47.92.37.168/supermarket/data/get_commodity_info.php"
		,async:true
		,data:{
			'id':pro_id
		}
		// ,dataType:"jsonp"
		// ,jsonp:"callback"
		,success:function(data){
			console.log(data);
			$('.content_img').eq(num).attr('src',img_url+data.img);
			
			$('.content_info').eq(num).text(data.name.slice(1,10)+"...");
			$('.price').eq(num).text("￥"+data.price);
			$('.total_price').eq(num).text("￥"+(data.price*$('.commodity_num').eq(num).text()).toFixed(2));
			num++;
			if(num==arr_id.length) return;
			getProDetails(arr_id[num]);//用递归函数来获取商品详情
		}
	});
}


//删除产品
var pro_id=0;
var _this=null;
$(document).on('click','.remove',function(){
	$('.overlay').css('display','block');
	$('.confirm').css({'opacity':'1','transform':'scale(1)'});
	pro_id=$(this).attr('id');
	_this=$(this);
})
//删除商品时点击确定按钮
$('.confirm_ok').click(function(){
	//删除数据表里的商品
	console.log(pro_id,username)
	$.ajax({
		type:"get"
		,url:"http://47.92.37.168/supermarket/data/my_commodity_delete_car.php"
		,async:true
		,data:{
			'commodity_id':pro_id,
			'user_phone':username
		}
		,dataType:"jsonp"
		,jsonp:"callback"
		,success:function(data){
			console.log(data);
			if (data[0].msg=='success') {
				_this.parents('.orderSection li').animate({'opacity':'0'},300,function(){
				
				_this.parents('.orderSection li').remove();//删除页面上此栏产品
					
				});

			}
		}
	});
	$('.overlay').css('display','none');
	$('.confirm').css({'opacity':'0','transform':'scale(0)'});
})
//删除商品时点击取消按钮
$('.confirm_cancel').click(function(){
	$('.overlay').css('display','none');
	$('.confirm').css({'opacity':'0','transform':'scale(0)'});
})

////点击评价订单
$(document).on('click','.evaluate_order',function(){
	sessionStorage.setItem('pro_id',$(this).attr('id'));
	sessionStorage.setItem('consignee_name',$(this).attr('consignee_name'));
	sessionStorage.setItem('img_src',$(this).parents('li').find('img.content_img').attr('src'));
	location.href="evaluate.html";
})
//
//
////点击商品图片跳转到订单详情页面
//$(document).on('click','.content_img',function(){
//	sessionStorage.setItem('order_details',$(this).parent().attr('json'));
//	location.href="order_details.html";
//})
//点击商品名跳转到订单详情页面
//$(document).on('click','.xq',function(){
//	sessionStorage.setItem('order_details',$(this).parent().attr('json'));
//	location.href="order_details.html";
//})

//取消默认事件
function preDef (evt) {
	var e=evt||window.event;
	if (e.preventDefault) {
		e.preventDefault();
	} else{
		e.returnValue=false;
	}
}